package com.ygs.secs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ygs.secs.model.Student;
import com.ygs.secs.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class StudentManagerController {
    @Autowired
    StudentService studentService;


    /**
     * 添加学生
     *
     * @param jsonStudent 学生json字符串
     * @return 操作结果
     */
    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addStudent(@RequestParam(value = "jsonStudent") String jsonStudent) {
        Map<String, Object> result = new HashMap<>();
        try {
            Student student = JSONObject.parseObject(jsonStudent, Student.class);
            int res = studentService.insert(student);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            result.put("code", 501);
            result.put("msg", "操作失败，主键冲突，请先修改重复ID");
            return result;
        }
    }

    /**
     * 删除学生
     *
     * @param stuId
     * @return
     */
    @RequestMapping("/delStudent")
    @ResponseBody
    public Map<String, Object> delStudent(@RequestParam("stuId") String stuId) throws Exception{
        Map<String, Object> result = new HashMap<>();
        try {
            int res = studentService.deleteByPrimaryKey(stuId);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }

    /**
     * 修改学生信息
     *
     * @param jsonStudent 学生实体接送字符串
     * @return 操作结果
     */
    @RequestMapping("/updateStudent")
    @ResponseBody
    public Map<String, Object> updateStudent(@RequestParam("jsonStudent") String jsonStudent) {
        Student student = JSONObject.parseObject(jsonStudent, Student.class);
        int res = studentService.updateByPrimaryKeySelective(student);
        Map<String, Object> result = new HashMap<>();
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "操作成功");
        } else {
            result.put("code", 500);
            result.put("msg", "操作失败");
        }
        return result;
    }

    /**
     * 根据条件查询学生
     *
     * @param jsonStudent 学生实体json串
     * @return 学生集合
     */
    @RequestMapping(value = "getStudentByAttributes")
    @ResponseBody
    public Map<String, Object> getStudentByAttributes(String jsonStudent,
                                                      @RequestParam(defaultValue = "1") int page,
                                                      @RequestParam(defaultValue = "20") int limit) {
        Map<String, Object> resultData = new HashMap<>();
        try {
            Student student = JSONObject.parseObject(jsonStudent, Student.class);
            Page startPage = PageHelper.startPage(page, limit);
            List<Student> students = studentService.getStudentByAttributes(student);
            //分页查询
            if (students.size()>0){
                resultData.put("code", 200);
                resultData.put("msg", "学生数据查询成功");
                resultData.put("data", students);
                resultData.put("count", startPage.getTotal());
            }else{
                resultData.put("code",201);
                resultData.put("msg","数据为null，请先添加");
            }
            return resultData;
        }catch (Exception e){
            resultData.put("code", 500);
            resultData.put("msg", "system error");
            return resultData;
        }
    }

    /**
     * 批量修改学生信息
     *
     * @param jsonArrayStudent 学生实体json数组串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchUpdateStudent",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchUpdateStudent(String jsonArrayStudent) {
        Map<String, Object> result = new HashMap<>();
        List<Student> students= JSONArray.parseArray(jsonArrayStudent,Student.class);
        int res = studentService.updateBatchSelective(students);
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "操作成功");
        } else {
            result.put("code", 500);
            result.put("msg", "操作失败");
        }
        return result;
    }

    /**
     * 批量添加学生信息
     *
     * @param jsonArrayStudent 学生数组json串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchInsertStudent",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchInsertStudent(String jsonArrayStudent) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Student> students= JSONArray.parseArray(jsonArrayStudent,Student.class);
            int res = studentService.batchInsert(students);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.put("code", 501);
            result.put("msg", "操作失败，主键冲突，请先修改重复ID");
            return result;
        }
    }

    /**
     * 批量删除学生信息
     *
     * @param students 学生id数组
     * @return 操作结果
     */
    @RequestMapping(value = "/batchDelStudent",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchDelStudent(String[] students)throws Exception {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = studentService.batchDelete(students);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }
}
